package com.book.workbench.controller;

import com.book.common.Result;
import com.book.workbench.pojo.DateRequire;
import com.book.workbench.pojo.DateReturn;
import com.book.workbench.pojo.Information;
import com.book.workbench.service.InformationService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class InformationController {
    @Autowired
    private InformationService informationService;

    @GetMapping("/workbench/infor/linecharts.do")
    public Map<String,Object> lineCharts(String date){
        return informationService.getCountByDate(date);
    }

    @PostMapping("/workbench/infor/borr.do")
    public Result addInfo(@RequestBody Information information){
        int addInfoResult = informationService.addInfo(information);
        if(addInfoResult==1){
            return Result.success();
        }else if(addInfoResult==0){
            return Result.error("系统错误");
        }else{
            return Result.error("该书已经被借出");
        }
    }

    @PostMapping("/workbench/info/back.do")
    public Result updateInfo(@RequestBody Information information){
        int updateInfoResult = informationService.updateInfo(information);
        if(updateInfoResult==1){
            return Result.success();
        }else if(updateInfoResult==0){
            return Result.error("系统错误");
        }else{
            return Result.error("未发现相应的借阅记录");
        }
    }

    @GetMapping("/workbench/info/allinfo.do")
    public Result searchInfoBy( DateRequire dateRequire){
        PageInfo<DateReturn> dateReturnPageInfo = informationService.searchInfoBy(dateRequire);
        int countSearchInfoBy = informationService.searchCountInfoBy(dateRequire);
        return Result.success(dateReturnPageInfo.getList(),countSearchInfoBy);
    }


}
